const express = require("express");
const router = express.Router();
const AllowanceDao = require("../database/AllowanceDao");

// 验证加班类型
const validateAllowanceType = (type) => {
    return type === '普通加班' || type === '节假日加班';
};

// 获取用户的加班记录列表
const getAllowanceByUserId = async (req, res) => {
    try {
        const userId = req.params.userId;
        if (!userId) {
            return res.status(400).json({
                code: 400,
                msg: '用户ID不能为空'
            });
        }

        const records = await AllowanceDao.getAllowanceByUserId(userId);
        res.json({
            code: 200,
            msg: '获取加班记录成功',
            data: records
        });
    } catch (error) {
        console.error('获取加班记录失败:', error);
        res.status(500).json({
            code: 500,
            msg: '获取加班记录失败: ' + (error.message || '未知错误')
        });
    }
};

// 添加加班记录
const addAllowance = async (req, res) => {
    try {
        const { user_id, allowance_hour, allowance_type, year_month } = req.body;

        // 验证必填字段
        if (!user_id || !allowance_hour || !allowance_type || !year_month) {
            return res.status(400).json({
                code: 400,
                msg: '缺少必填字段'
            });
        }

        // 验证加班类型
        if (!validateAllowanceType(allowance_type)) {
            return res.status(400).json({
                code: 400,
                msg: '无效的加班类型'
            });
        }

        const result = await AllowanceDao.addAllowance({
            user_id,
            allowance_hour,
            allowance_type,
            year_month
        });

        res.json({
            code: 200,
            msg: '添加加班记录成功',
            data: result
        });
    } catch (error) {
        console.error('添加加班记录失败:', error);
        res.status(500).json({
            code: 500,
            msg: '添加加班记录失败: ' + (error.message || '未知错误')
        });
    }
};

// 更新加班记录
const updateAllowance = async (req, res) => {
    try {
        const id = req.params.id;
        const { user_id, allowance_hour, allowance_type, year_month } = req.body;

        // 验证必填字段
        if (!id || !user_id || !allowance_hour || !allowance_type || !year_month) {
            return res.status(400).json({
                code: 400,
                msg: '缺少必填字段'
            });
        }

        // 验证加班类型
        if (!validateAllowanceType(allowance_type)) {
            return res.status(400).json({
                code: 400,
                msg: '无效的加班类型'
            });
        }

        const result = await AllowanceDao.updateAllowance(id, {
            user_id,
            allowance_hour,
            allowance_type,
            year_month
        });

        res.json({
            code: 200,
            msg: '更新加班记录成功',
            data: result
        });
    } catch (error) {
        console.error('更新加班记录失败:', error);
        res.status(500).json({
            code: 500,
            msg: '更新加班记录失败: ' + (error.message || '未知错误')
        });
    }
};

// 删除加班记录
const deleteAllowance = async (req, res) => {
    try {
        const id = req.params.id;
        const { user_id } = req.body;

        if (!id || !user_id) {
            return res.status(400).json({
                code: 400,
                msg: '缺少必填字段'
            });
        }

        const result = await AllowanceDao.deleteAllowance(id, user_id);
        res.json({
            code: 200,
            msg: '删除加班记录成功',
            data: result
        });
    } catch (error) {
        console.error('删除加班记录失败:', error);
        res.status(500).json({
            code: 500,
            msg: '删除加班记录失败: ' + (error.message || '未知错误')
        });
    }
};

// 获取单条加班记录
const getAllowanceById = async (req, res) => {
    try {
        const id = req.params.id;
        const userId = req.params.userId;

        if (!id || !userId) {
            return res.status(400).json({
                code: 400,
                msg: '缺少必填字段'
            });
        }

        const record = await AllowanceDao.getAllowanceById(id, userId);
        if (!record) {
            return res.status(404).json({
                code: 404,
                msg: '加班记录不存在'
            });
        }

        res.json({
            code: 200,
            msg: '获取加班记录成功',
            data: record
        });
    } catch (error) {
        console.error('获取加班记录失败:', error);
        res.status(500).json({
            code: 500,
            msg: '获取加班记录失败: ' + (error.message || '未知错误')
        });
    }
};

module.exports = {
    getAllowanceByUserId,
    addAllowance,
    updateAllowance,
    deleteAllowance,
    getAllowanceById
};